package com.hippo.core.ui.compose.login.forget

import android.util.Log
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.hippo.core.R
import com.hippo.core.core.ext.paddingBody
import com.hippo.core.ui.components.CommonBody
import com.hippo.core.ui.components.InputEdit
import com.hippo.core.ui.components.PasswordEdit
import com.hippo.core.ui.components.ThemeButton
import com.hippo.core.ui.components.VerticalSpacer
import com.hippo.core.ui.theme.HippoTheme
import kotlinx.coroutines.launch

@Composable
fun ResetPasswordScreen(
    email: String,
    code: String,
    onBack: () -> Unit,
    onNext: () -> Unit,
    vm: ForgetVM = viewModel()
) {

    Log.i("sss", "ResetPasswordScreen: email: $email, code: $code")

    val uiState = vm.uiState
    val scope = rememberCoroutineScope()

    CommonBody(
        title = stringResource(R.string.verify_9),
        onBack = onBack
    ) {

        Column(
            modifier = Modifier
                .fillMaxSize()
                .paddingBody()
        ) {

            VerticalSpacer(30.dp)

            Text(
                text = stringResource(R.string.verify_10),
                style = MaterialTheme.typography.labelLarge
            )

            VerticalSpacer(24.dp)



            InputEdit(
                enable = false,
                text = email,
                hint = stringResource(R.string.login_2),
                onValueChange = {},
                keyboardOptions = KeyboardOptions.Default.copy(
                    imeAction = ImeAction.Next,
                    keyboardType = KeyboardType.Email
                ),
                leftPainter = painterResource(R.mipmap.mail)
            )

            VerticalSpacer(10.dp)

            PasswordEdit(
                text = uiState.password.orEmpty(),
                hint = stringResource(R.string.verify_11),
                onValueChange = {
                    vm.updateState(
                        uiState.copy(
                            password = it
                        )
                    )
                },
                leftPainter = painterResource(R.mipmap.pwd),
            )
            VerticalSpacer(10.dp)

            PasswordEdit(
                text = uiState.passwordConfirm.orEmpty(),
                hint = stringResource(R.string.verify_13),
                onValueChange = {
                    vm.updateState(
                        uiState.copy(
                            passwordConfirm = it
                        )
                    )
                },
                leftPainter = painterResource(R.mipmap.pwd),
            )


            VerticalSpacer(50.dp)

            ThemeButton(
                modifier = Modifier.fillMaxWidth(),
                text = stringResource(R.string.next),
                onClick = {
                    scope.launch {
                        vm.resetPassword {
                            if (it) {
                                onNext()
                            }
                        }
                    }
                }
            )

        }
    }

}


@Preview(showBackground = true)
@Composable
private fun ResetPasswordScreenPreview() {

    HippoTheme {
        ResetPasswordScreen("aa@qq.com","1234",{}, {})
    }

}